package com.tdrc.common.service;

import com.tdrc.common.beans.Permission;
import com.tdrc.common.beans.User;
import com.tdrc.common.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author dpf
 * @version 1.0
 * @date 2020-4-13 15:25
 * @instruction ...
 */
@Service
public class UserService implements  IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteUser(userId);
    }

    @Override
    public List<Map<String, Object>> listPageUser(Map<String, Object> map) {
        return userMapper.listPageUser(map);
    }

    @Override
    public long checkLogNameUnique(String logName) {
        long longNameCount = userMapper.checkLogNameUnique(logName);
        if(longNameCount>0){
            return 2007 ;
        }else{
            return longNameCount;
        }
    }

    @Override
    public long checkUserNameUnique(String userName) {
        long userNameCount = userMapper.checkUserNameUnique(userName);
        if(userNameCount>0){
            return  2006;
        }else{
            return userNameCount;
        }
    }

    @Override
    public void updateUserStatus(User user) {
        userMapper.updateUserStatus(user);
    }

    @Override
    public List<Map<String, Object>> getRoleList(Integer userId) {
        return userMapper.getRoleList(userId);
    }

    @Override
    public int deleteUserRoleByUserId(Integer userId) {
      return    userMapper.deleteUserRoleByUserId(userId);
    }

    @Override
    public void addUserRole(List<Integer> roles, Integer userId) {
        userMapper.addUserRole(roles,userId);
    }

    @Override
    public User getUserByLogName(String logName) {
        return userMapper.getUserByLogName(logName);
    }

    @Override
    public List<Permission> getRolesByUserId(Integer userId) {
        List<Permission> listPermission = userMapper.getRolesByUserId(userId);
        for(Permission permission:listPermission){
            Set<Permission.ActionEntity> actionEntityList = userMapper.getPermissionByMenuId(permission.getRoleId(),permission.getMenuId());
            permission.setActionEntitySet(actionEntityList);
        }
        return listPermission;
    }

    @Override
    public Map<String, Object> getUserInfo(Integer userId) {
        Map<String, Object> user = userMapper.getUserInfo(userId);
        Map<String,Object> permissions=new HashMap<String,Object>(16);
        permissions.put("permissions",this.getRolesByUserId(userId));

        user.put("role",permissions);
        return user;
    }
}
